<!--
 * @Descripttion: 
 * @Version: 
 * @Author: gaohj
 * @Date: 2023-02-09 17:17:42
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2023-02-09 17:41:51
-->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        div{
            width: 100px;
            height: 100px;
            background-color: skyblue;
            position: absolute;
            left: 200px;
            top: 130px;
            cursor: pointer;
            opacity: 1;
        }
    </style>
</head>
<body>
    <div></div>
    <script>
       //  opacity 没有px 
       // 0 ~1 
       // 没有运动过程  要么 0 要么 1 要么0.5 


       // 解决方案 : 
        // 改变计算方式:
        // 1. 放大100倍 让他有运动过程
        // 2.设置的时候 除以100
        // 这样的话取值范围就是 0~100




        function move(ele,type,target){
            if(type == 'opacity'){ target = target*100;} // 目标距离  目标值 
            const timer = setInterval(()=>{
                // 初始值
                var init;
                if(type !== 'opacity'){
                    init = parseInt(window.getComputedStyle(ele)[type]); // 初始距离
                }else{
                    init = window.getComputedStyle(ele)[type]*100 ; // 初始透明度
                }

                let duration = (target - init)/10;
                duration = duration>0?Math.ceil(duration):Math.floor(duration);
                ele.style[type] = init + 'px';
                
                if(init===target){
                    clearInterval(timer);
                }else{
                    if(type === 'opacity'){
                        ele.style[type] = (init +duration)/100;
                        return;
                    }
                    ele.style[type] = init +duration+ 'px';
                }
            },50)
        }

        const divEle = document.querySelector('div');
        divEle.onclick = function(){
            move(divEle,'opacity',0);
        }

        // 可以left top width height 
        // opacity bordeRadius

        // 参数两个即可 
        // 元素 对象 
        
    </script>
</body>
</html>